package com.px.framework.mqtt.model;


import java.util.Arrays;
import java.util.regex.Pattern;

import static com.px.framework.mqtt.model.TopicConst.*;

/**
 * @author WRJ
 * @version 1.3
 * @date 2024-08
 */

public enum DeviceTopicEnum {

    STATUS(Pattern.compile("^" + BASIC_PRE + PRODUCT + REGEX_SN + STATUS_SUF + "$"), BASIC_PRE + STATUS_SUF),

    STATE(Pattern.compile("^" + THING_MODEL_PRE + PRODUCT + REGEX_SN + STATE_SUF + "$"), THING_MODEL_PRE + STATE_SUF),

    SERVICE_REPLY(Pattern.compile("^" + THING_MODEL_PRE + PRODUCT + REGEX_SN + SERVICES_SUF + _REPLY_SUF + "$"), THING_MODEL_PRE + SERVICES_SUF + _REPLY_SUF),

    OSD(Pattern.compile("^" + THING_MODEL_PRE + PRODUCT + REGEX_SN + OSD_SUF + "$"), THING_MODEL_PRE + OSD_SUF),

    REQUESTS(Pattern.compile("^" + THING_MODEL_PRE + PRODUCT + REGEX_SN + REQUESTS_SUF + "$"), THING_MODEL_PRE + REQUESTS_SUF),

    EVENTS(Pattern.compile("^" + THING_MODEL_PRE + PRODUCT + REGEX_SN + EVENTS_SUF + "$"), THING_MODEL_PRE + EVENTS_SUF),

    PROPERTY_SET_REPLY(Pattern.compile("^" + THING_MODEL_PRE + PRODUCT + REGEX_SN + PROPERTY_SUF + SET_SUF + _REPLY_SUF + "$"), THING_MODEL_PRE + PROPERTY_SUF + SET_SUF + _REPLY_SUF),

    APP_TOPIC(Pattern.compile("^" + WRJ + APP + REGEX_SN + _SERVICES + "$"), WRJ + APP + _SERVICES),

    APP_PLAN_TOPIC(Pattern.compile("^" + WRJ + SERVICES_ + REGEX_SN + _ACTION + "$"), WRJ + SERVICES_ + _ACTION),

    UNKNOWN(Pattern.compile("^.*$"), "DEFAULT");

    ;

    Pattern pattern;

    String beanName;

    public Pattern getPattern() {
        return pattern;
    }

    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

    public String getBeanName() {
        return beanName;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    DeviceTopicEnum(Pattern pattern, String beanName) {
        this.pattern = pattern;
        this.beanName = beanName;
    }

    public static DeviceTopicEnum find(String topic) {
        return Arrays.stream(DeviceTopicEnum.values()).filter(topicEnum -> topicEnum.pattern.matcher(topic).matches()).findAny().orElse(UNKNOWN);
    }
}
